package com.yt.sys.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.sys.models.entity.SysAdmin;
import com.yt.sys.models.service.ISysAdminService;
import com.yt.sys.server.mapper.SysAdminMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService(interfaceClass = ISysAdminService.class)
public class SysAdminServiceImpl extends ServiceImpl<SysAdminMapper, SysAdmin> implements ISysAdminService {

    /**
     * 根据用户名查找管理员
     * @param username 用户名
     * @return 管理员信息
     */
    public SysAdmin getByUsername(String username) {
        QueryWrapper<SysAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("is_delete", 0);
        return this.getOne(queryWrapper);
    }

    /**
     * 获取所有未删除的管理员
     * @return 管理员列表
     */
    public List<SysAdmin> getAllActiveAdmins() {
        QueryWrapper<SysAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("create_time");
        return this.list(queryWrapper);
    }

    /**
     * 禁用管理员
     * @param adminId 管理员ID
     * @return 是否操作成功
     */
    public boolean disableAdmin(Integer adminId) {
        SysAdmin admin = this.getById(adminId);
        if (admin == null || admin.getIsDelete() == 1) {
            return false;
        }
        admin.setStatus(0);
        return this.updateById(admin);
    }

    /**
     * 启用管理员
     * @param adminId 管理员ID
     * @return 是否操作成功
     */
    public boolean enableAdmin(Integer adminId) {
        SysAdmin admin = this.getById(adminId);
        if (admin == null || admin.getIsDelete() == 1) {
            return false;
        }
        admin.setStatus(1);
        return this.updateById(admin);
    }
}